package com.zbec.mdrs.util;

import com.zbec.mdrs.constants.Constants;
import com.zbec.mdrs.entity.RecordEntity;
import org.apache.commons.beanutils.PropertyUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;

public class ValidationUtils {


    public static String validateDiscountCell(String property) {
        String[] ss = Constants.VALIDATION_FIELDS_DISCOUNT.get(property);
        return validateCell(ss);
    }

    public static String validateLoanCell(String property) {
        String[] ss = Constants.VALIDATION_FIELDS_LOAN.get(property);
        return validateCell(ss);
    }

    public static String validateDraftsCell(String property) {
        String[] ss = Constants.VALIDATION_FIELDS_DRAFTS.get(property);
        return validateCell(ss);
    }

    public static String validateEntrustedLoanCell(String property) {
        String[] ss = Constants.VALIDATION_FIELDS_ENTRUSTED_LOAN.get(property);
        return validateCell(ss);
    }

    public static String validateGuaranteeCell(String property) {
        String[] ss = Constants.VALIDATION_FIELDS_GUARANTEE.get(property);
        return validateCell(ss);
    }

    private static String validateCell(String[] ss) {
        String r = "";
        String c = "";
        if (ss != null) {
            for (String s : ss) {
//                if ("M".equals(s)) {
//                    c = c + "required ";
//                }
                if ("N".equals(s)) {
                    c = c + "digits ";
                }
                if ("LL".equals(s)) {
                    c = c + "LL ";
                }
                if ("JE".equals(s)) {
                    c = c + "JE ";
                }
                if (s.startsWith("D")) {
                    c = c +"date ";
                }
                if (s.startsWith("L:")) {
                    r = r+ " maxlength="+s.split(":")[1];
                }
            }
        }
        if (c.length() > 0) {
            c = " class='"+c+"' ";
            r = r + c;
        }
        return r;

    }

    public static void validateData(String bizType, RecordEntity re) {
        String errorDesc = re.getErrorDesc()!=null?re.getErrorDesc():"";
//        if (errorDesc.indexOf("未匹配到关键信息") == -1 ) {
//            errorDesc = "";
//        }
        if(!re.isAdd()){
            errorDesc = "";
        }
        String missedField = re.getMissedField()!=null?re.getMissedField():"";

        boolean m = false;
        boolean l = false;
        boolean ll = false;
        int lz = 0;



        Map mp = null;
        String bt = null;
        if(Constants.BUSINESS_TYPE_DRAFTS.equals(bizType)) {
            mp =  Constants.VALIDATION_FIELDS_DRAFTS;
            bt = "drafts";
        }else if (Constants.BUSINESS_TYPE_LOAN.equals(bizType)) {
            mp =  Constants.VALIDATION_FIELDS_LOAN;
            bt = "loan";
        }else if (Constants.BUSINESS_TYPE_DISCOUNT.equals(bizType)) {
            mp =  Constants.VALIDATION_FIELDS_DISCOUNT;
            bt = "discount";
        }else if (Constants.BUSINESS_TYPE_ENTRUSTED_LOAN.equals(bizType)) {
            mp =  Constants.VALIDATION_FIELDS_ENTRUSTED_LOAN;
            bt = "entrustedLoan";
        }else if (Constants.BUSINESS_TYPE_GUARANTEE.equals(bizType)) {
            mp =  Constants.VALIDATION_FIELDS_GUARANTEE;
            bt = "guarantee";
        }


        for (Iterator it = mp.keySet().iterator(); it.hasNext();) {
            String key = (String) it.next();
            String[] ss = (String[]) mp.get(key);
            for (String s : ss) {
                if ("M".equals(s)) {
                    m = true;
                }
                if (s.startsWith("L:")) {
                    l = true;
                    lz = Integer.parseInt(s.split(":")[1]);
                }
                if ("LL".equals(s)) {
                    ll = true;
                }
            }
            try {
                Object v = PropertyUtils.getProperty(re, key);
                if(v==null||"".equals(v)) {
                    if (m) {
                        String err = ResourceUtils.getLocaleString("label." + bizType + "." + key)+ResourceUtils.getLocaleString("message.error.mandatory");
                        errorDesc = errorDesc + err+";";
                    }
                    if (missedField.indexOf(key) == -1) {
                        missedField = missedField + key + ",";
                    }

                }
                if (l) {
                    if (v!=null) {
                        if (!(v instanceof Date)&&String.valueOf(v).length() > lz) {
                            String err =ResourceUtils.getLocaleString("label." + bizType + "." + key) + ResourceUtils.getLocaleString("message.error.length") + lz+"位";
                            errorDesc = errorDesc + err + ";";

                            if (missedField.indexOf(key) == -1) {
                                missedField = missedField + key + ",";
                            }                                                        
                        }
                    }
                }
                if(ll){
                    if (v!=null) {
                        if( v instanceof BigDecimal ){
                            BigDecimal b=(BigDecimal)v;
                            int result=b.compareTo(new BigDecimal(100));
                            if(result==1){
                                String err =ResourceUtils.getLocaleString("label." + bizType + "." + key) + ResourceUtils.getLocaleString("message.error.ll");
                                errorDesc = errorDesc + err + ";";
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            m = false;
            l = false;
            ll=false;
        }

        
        re.setMissedField(missedField);
        re.setErrorDesc(errorDesc);
        if(errorDesc!=null&&!"".equals(errorDesc)){
//            if (errorDesc.indexOf("未匹配到关键信息") != -1) {
//                re.setExistError(Constants.EXIST_ERROR_NO);
//            } else {
                re.setExistError(Constants.EXIST_ERROR_YES);
//            }
        }else{
            re.setExistError(Constants.EXIST_ERROR_NO);
        }
    }

}
